package dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import dao.BookDao;
import vo.Book;

@Repository
public class BookDaoImpl implements BookDao {
	@PersistenceContext(name = "un")
	private EntityManager em;

	@Override
	public Book addBook(Book book) {
		try{
			em.persist(book);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return book;
	}

	@Override
	public void deleteBook(int id) {
		Book book = em.find(Book.class, id);
		em.remove(book);

	}

	@Override
	public List<Book> findAllBooks() {
		String jpql="from Book";
		List<Book> books=em.createQuery(jpql).getResultList();
		return books;
	}

}
